package top.xuziyi.crawler.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.xuziyi.common.pojo.StockDayAfter;
import top.xuziyi.common.pojo.StockDayNormal;
import top.xuziyi.common.pojo.StockMonth;
import top.xuziyi.common.pojo.StockWeek;
import top.xuziyi.crawler.dao.StockDayAfterDao;
import top.xuziyi.crawler.dao.StockDayNormalDao;
import top.xuziyi.crawler.dao.StockMonthDao;
import top.xuziyi.crawler.dao.StockWeekDao;

import java.util.List;

@Service
public class StockService {

    @Autowired
    private StockDayNormalDao stockDayNormalDao;
    @Autowired
    private StockDayAfterDao stockDayAfterDao;
    @Autowired
    private StockWeekDao stockWeekDao;
    @Autowired
    private StockMonthDao stockMonthDao;


    /**
     * 如果有就不操作,如果没有就新增
     *
     * @param list
     */
    @Transactional
    public void batchSaveStockDayNormal(List<StockDayNormal> list) {
        for (StockDayNormal stockDayNormal : list) {
            if (stockDayNormalDao.findBySymbolAndDate(stockDayNormal.getSymbol(),
                    stockDayNormal.getDate()) == 0) {
                stockDayNormalDao.save(stockDayNormal);
            }
        }
    }

    /**
     * 无论有没有都会进行修改或者新增
     *
     * @param list
     */
    @Transactional
    public void batchSaveStockDayNormal2(List<StockDayNormal> list) {
        for (StockDayNormal stockDayNormal : list) {
            stockDayNormalDao.save(stockDayNormal);
        }
    }

    /**
     * 如果有就不操作,如果没有就新增
     *
     * @param list
     */
    @Transactional
    public void batchSaveStockDayAfter(List<StockDayAfter> list) {
        for (StockDayAfter stockDayAfter : list) {
            if (stockDayAfterDao.findBySymbolAndDate(stockDayAfter.getSymbol(),
                    stockDayAfter.getDate()) == 0) {
                stockDayAfterDao.save(stockDayAfter);
            }
        }
    }

    /**
     * 无论有没有都会进行修改或者新增
     *
     * @param list
     */
    @Transactional
    public void batchSaveStockDayAfter2(List<StockDayAfter> list) {
        for (StockDayAfter stockDayAfter : list) {
            stockDayAfterDao.save(stockDayAfter);
        }
    }

    /**
     * 如果有就不操作,如果没有就新增
     *
     * @param list
     */
    @Transactional
    public void batchSaveStockWeek(List<StockWeek> list) {
        for (StockWeek stockWeek : list) {
            if (stockWeekDao.findBySymbolAndDate(stockWeek.getSymbol(), stockWeek.getDate()) == 0) {
                stockWeekDao.save(stockWeek);
            }
        }
    }

    /**
     * 无论有没有都会进行修改或者新增
     *
     * @param list
     */
    @Transactional
    public void batchSaveStockWeek2(List<StockWeek> list) {
        for (StockWeek stockWeek : list) {
            stockWeekDao.save(stockWeek);
        }
    }

    /**
     * 如果有就不操作,如果没有就新增
     *
     * @param list
     */
    @Transactional
    public void batchSaveStockMonth(List<StockMonth> list) {
        for (StockMonth stockMonth : list) {
            if (stockMonthDao.findBySymbolAndDate(stockMonth.getSymbol(), stockMonth.getDate()) == 0) {
                stockMonthDao.save(stockMonth);
            }
        }
    }

    /**
     * 无论有没有都会进行修改或者新增
     *
     * @param list
     */
    @Transactional
    public void batchSaveStockMonth2(List<StockMonth> list) {
        for (StockMonth stockMonth : list) {
            stockMonthDao.save(stockMonth);
        }
    }
}
